package com.achilles.netease;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class DiscPaneView extends RelativeLayout {

    private ImageView mIvDisc;
    private ImageView mNeedle;
    private View mParent;
    private final float DistSizeScale = (float) (720.0 / 1080.0);
    private final float PicMusicScale = (float) (472.0 / 1080.0);
    private final int NeedleRotationBegin = -25;
    private int mScreenWidth;
    private ObjectAnimator mDiscObjectAnimator, mNeedleObjectAnimator;
    private boolean mIsInitOrPause = true;
    private boolean mIsNeedleObjectAnimatorReverse = false;

    public DiscPaneView(Context context) {
        this(context, null);
    }

    public DiscPaneView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DiscPaneView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public DiscPaneView(Context context, AttributeSet attrs, int defStyleAttr,
                        int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        // TODO Auto-generated constructor stub
        mParent = LayoutInflater.from(context).inflate(R.layout.disc_view,
                this, true);
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;

        mNeedle = (ImageView) mParent.findViewById(R.id.ivNeedle);
        mIvDisc = (ImageView) mParent.findViewById(R.id.ivDisc);

        initView();
        initAnimator();
    }

    private void initAnimator() {
        mDiscObjectAnimator = ObjectAnimator.ofFloat(mIvDisc,"rotation",0,360);
        mDiscObjectAnimator.setDuration(20 * 1000);
        mDiscObjectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        mDiscObjectAnimator.setRepeatMode(ValueAnimator.INFINITE);
        mDiscObjectAnimator.setInterpolator(new LinearInterpolator());
        mDiscObjectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                mIsInitOrPause = false;
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {
                mIsInitOrPause = true;
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        mNeedleObjectAnimator = ObjectAnimator.ofFloat(mNeedle,"rotation",NeedleRotationBegin,0);
        mNeedleObjectAnimator.setDuration(500);
        mNeedleObjectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if(!mIsNeedleObjectAnimatorReverse) {
                    beginDiscAnimator();
                    MusicPlayManager.getInstance().play(getContext());
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        mNeedleObjectAnimator.setInterpolator(new LinearInterpolator());
    }

    private void initView() {
        int picMusicSize = (int) (PicMusicScale * mScreenWidth);
        int discSize = (int) (DistSizeScale * mScreenWidth);

        Bitmap discBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource
                (getResources(), R.drawable.play_disc), discSize, discSize, true);

        Bitmap picMusicBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource
                (getResources(), R.drawable.pic_music), picMusicSize, picMusicSize, true);
        RoundedBitmapDrawable discRoundedBitmapDrawable = RoundedBitmapDrawableFactory.create
                (getResources(), discBitmap);

        discRoundedBitmapDrawable.setCircular(true);
        discRoundedBitmapDrawable.setAntiAlias(true);

        RoundedBitmapDrawable picMusicRoundedBitmapDrawable = RoundedBitmapDrawableFactory.create
                (getResources(), picMusicBitmap);
        picMusicRoundedBitmapDrawable.setCircular(true);
        picMusicRoundedBitmapDrawable.setAntiAlias(true);

        Drawable[] layers = new Drawable[2];
        layers[0] = picMusicRoundedBitmapDrawable;
        layers[1] = discRoundedBitmapDrawable;

        LayerDrawable layerDrawable = new LayerDrawable(layers);
        int inset = (int) ((DistSizeScale - PicMusicScale) * mScreenWidth /2);
        layerDrawable.setLayerInset(0,inset,inset,inset,inset);
        mIvDisc.setImageDrawable(layerDrawable);

        mNeedle.setPivotX(50);
        mNeedle.setPivotY(50);
        mNeedle.setRotation(NeedleRotationBegin);
    }

    private void beginDiscAnimator() {
        if(mIsInitOrPause) {
            mDiscObjectAnimator.start();
        }else {
            mDiscObjectAnimator.resume();
        }
    }

    public void play() {
        mNeedleObjectAnimator.start();
        mIsNeedleObjectAnimatorReverse = false;
    }

    public void pasue() {
        mDiscObjectAnimator.pause();
        mNeedleObjectAnimator.reverse();
        mIsNeedleObjectAnimatorReverse = true;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        mDiscObjectAnimator.cancel();
        mIvDisc.clearAnimation();
    }
}
